$(function () {
    $("#jqGrid").jqGrid({
        url: '../datapayroll/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', width: 50, key: true ,hidden:true},
			{ label: '', name: 'employeeid', name: 'employeeid',width: 80,hidden:true }, 			
			{ label: '发薪日', name: 'payrollmonth', width: 80,hidden:true }, 
			{ label: '姓名', name: 'name', index: 'name',width: 100,frozen: true }, 			
			{ label: '工号', name: 'workNo', index: 'workNo',width: 100 ,frozen: true}, 			
			{ label: '职位编号', name: 'jobno',index: 'jobno', width: 60 ,frozen: true}, 
			{ label: '首月底薪', name: 'firstmonthwage',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '底薪', name: 'monthwage',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '端口津贴', name: 'portfee', formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			},width: 80 }, 			
			{ label: '话费补助', name: 'mobilefee', formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			},width: 80 }, 			
			{ label: '业务提成', name: 'businesspromotion',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 },
			{ label: '单例收入', name: 'separateMoney', formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			},width: 80 }, 		
			{ label: '新人卓越奖励', name: 'newtalent', width: 80,hidden:true }, 			
			{ label: '新人推荐奖励', name: 'newrecommend',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 
			{ label: '行程排名', name: 'order',index: 'order', width: 60 ,frozen: true},
			{ label: '职务津贴', name: 'performanceMoney', formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			},width: 80 },
			{ label: '主管直接', name: 'directordirect',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '主管间接', name: 'directorindirect',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			/*{ label: '职务津贴', name: 'joballowance',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, */			
			{ label: '分行直接', name: 'branchdirect',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '分行间接', name: 'branchindirect',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '区域直接', name: 'areadirect',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '区域间接', name: 'areaindirect',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '大区直接', name: 'bigareadirect',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '大区间接', name: 'bigareaindirect',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '社保', name: 'socialinsurance',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 			
			{ label: '公积金', name: 'housingfund',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 },
			{ label: '加款', name: 'reward', formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			},width: 80 }, 			
			{ label: '扣款', name: 'punishment', formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			},width: 80 }, 			
			{ label: '总计', name: 'totalMoney',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }		
        ],
        viewrecords: true,
        height: $(window).height()-155,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            sort:"sort",//需要排序的列
            order: "order"
        },
        ondblClickRow:function(row){  
            var rowData = $('#jqGrid').jqGrid('getRowData',row);  
            look(rowData.workNo,rowData.payrollmonth);  
        }, 
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	var ids=$("#jqGrid").jqGrid("getDataIDs");
        	if(ids==null || ids.length==0){
        		vm.month="";
        	}else{
        		var rowData = $("#jqGrid").jqGrid("getRowData", ids[0]);
        		vm.month=rowData.payrollmonth;
        	}
        	/*$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); */
        }
        
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		value:null,
		calTime:null,
		month:"",
		dataPayroll: {
			
		}
	},
	methods: {
		query: function () {
			vm.calTime=$("#calTime").val();
			
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dataPayroll = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.dataPayroll.id == null ? "../datapayroll/save" : "../datapayroll/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.dataPayroll),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../datapayroll/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../datapayroll/info/"+id, function(r){
                vm.dataPayroll = r.dataPayroll;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'value': vm.value,"calTime":vm.calTime},
                page:1
            }).trigger("reloadGrid");
		},
		outExcel:function() {
		    var params = {"calTime":$("#calTime").val()};
		    postExcelFile(params, "../datapayroll/outExcel");
		  }
	}
});
function look(workNo,payrollmonth) {
	top.layer.open({
		type : 2,
		title : '工资明细',
		area : [ '80%', '90%' ],
		//scrollbar: true,
		shadeClose : false,
		content : 'zjdc/datapayrolldetail.html?workNo=' +workNo+'&month='+payrollmonth,
		btn : [ '确认', '取消' ],
		yes : function(index, layero) {
			top.layer.close(index);
		}
	});
}
function formatCurrency(num) 
{
	if(num)
	{
		//将num中的$,去掉，将num变成一个纯粹的数据格式字符串
		num = num.toString().replace(/\$|\,/g,'');
		//如果num不是数字，则将num置0，并返回
		if(''==num || isNaN(num)){return 'Not a Number ! ';}
		//如果num是负数，则获取她的符号
		var sign = num.indexOf("-")> 0 ? '-' : '';
		//如果存在小数点，则获取数字的小数部分
		var cents = num.indexOf(".")> 0 ? num.substr(num.indexOf(".")) : '';
		cents = cents.length>1 ? cents : '' ;//注意：这里如果是使用change方法不断的调用，小数是输入不了的
		//获取数字的整数数部分
		num = num.indexOf(".")>0 ? num.substring(0,(num.indexOf("."))) : num ;
		//如果没有小数点，整数部分不能以0开头
		if('' == cents){ if(num.length>1 && '0' == num.substr(0,1)){return 'Not a Number ! ';}}
		//如果有小数点，且整数的部分的长度大于1，则整数部分不能以0开头
		else{if(num.length>1 && '0' == num.substr(0,1)){return 'Not a Number ! ';}}
		//针对整数部分进行格式化处理，这是此方法的核心，也是稍难理解的一个地方，逆向的来思考或者采用简单的事例来实现就容易多了
		/*
               也可以这样想象，现在有一串数字字符串在你面前，如果让你给他家千分位的逗号的话，你是怎么来思考和操作的?
               字符串长度为0/1/2/3时都不用添加
               字符串长度大于3的时候，从右往左数，有三位字符就加一个逗号，然后继续往前数，直到不到往前数少于三位字符为止
		 */
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		{
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		}
		//将数据（符号、整数部分、小数部分）整体组合返回
		return (sign + num + cents);    
	}
}
function postExcelFile(params, url) { //params是post请求需要的参数，url是请求url地址
    var form = document.createElement("form");
    form.style.display = 'none';
    form.action = url;
    form.method = "post";
    document.body.appendChild(form);

    for(var key in params){
      var input = document.createElement("input");
      input.type = "hidden";
      input.name = key;
      input.value = params[key];
      form.appendChild(input);
    }

    form.submit();
    form.remove();
  }